class Solution {
public:
    vector<int> findClosestElements(vector<int>& arr, int k, int x) {
        int left = 0;
        int right = arr.size() - 1;
        int index = arr.size() - k;
        while (index--)
        {
            int a = abs(arr[left] - x);
            int b = abs(arr[right] - x);
            if (a <= b) right--;
            else left++;
        }
        vector<int> v;
        for (int i = left; i < left + k; i++)
        {
            v.push_back(arr[i]);
        }
        return v;
    }
};
